<template>
  <div class="banner-box">
    <div class="wrapper banner-inner">
      <button class="banner-btn left" @click="prev" aria-label="上一张"><Icon style="transform: rotate(-180deg)" name="icon-right"></Icon></button>
      <div class="banner-img-box">
        <img
          :src="data.data[activeIndex]?.photo"
          alt="banner"
          class="banner-img"
          style="cursor:pointer;"
          @click="onBannerClick"
        />
      </div>
      <button class="banner-btn right" @click="next" aria-label="下一张"><Icon name="icon-right"></Icon></button>
    </div>
  </div>
</template>

<script setup lang="ts">
import {getHomeBanner} from "@/api/home";
import { ref, onMounted, onUnmounted } from 'vue'
const {data} = await getHomeBanner()

const activeIndex = ref(0)
let timer: number | undefined

const router = useRouter()
function onBannerClick() {
  const banner = data.value?.data[activeIndex.value]
  if (banner && banner.url) {
    if(banner.url.startsWith('http')) {
      window.open(banner.url, '_blank')
    } else {
      window.open(banner.url,'_blank')
    }
  }
}

function prev() {
  if (!data.value || data.value.data.length === 0) return;
  activeIndex.value = (activeIndex.value - 1 + data.value.data.length) % data.value.data.length
}

function next() {
  if (!data.value || data.value.data.length === 0) return;
  activeIndex.value = (activeIndex.value + 1) % data.value.data.length
}

function startAutoPlay() {
  timer = window.setInterval(() => {
    next()
  }, 5000)
}

function stopAutoPlay() {
  if (timer) {
    clearInterval(timer)
    timer = undefined
  }
}

onMounted(() => {
  startAutoPlay()
})

onUnmounted(() => {
  stopAutoPlay()
})
</script>

<style scoped lang="less">
.banner-box {
  width: 100%;
  border-radius: 12px;
  overflow: hidden;
}
.banner-inner {
  position: relative;
  height: 528px;
  padding: 24px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.banner-img-box {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.banner-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 12px;
}
.banner-content {
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  text-align: center;
  color: #fff;
  z-index: 2;
  pointer-events: none;
}

.banner-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: rgba(23, 22, 22, 0.2);
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
  &:hover {
    background: rgba(23, 22, 22, 0.5);
  }
}
.banner-btn.left {
  left: 16px;
}
.banner-btn.right {
  right: 16px;
}
@media (max-width: 900px) {
  .banner-inner {
    height: 200px;
    border-radius: 6px;
  }
  .banner-title {
    font-size: 20px;
  }
  .banner-sub {
    font-size: 14px;
  }
  .banner-btn {
    width: 28px;
    height: 28px;
    font-size: 16px;
  }
}
</style>